import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/index.js'
Vue.use(Router)

import { routerListFun , routerListStr } from './Jurisdiction'
import Layout from '@/layout/index'

// 格式化 按需加载
function _import (file){
    return () => import('@/views/' + file + '.vue')
}
/**
 *  let arr=[1,2,3,20,21,22]   //权限列表数组（有后台返回）
 * 路由书写规则
 *    1、只有一级路由（实际为二级路由）：
 *     {
          id: 2,                                  //ID全局不能重复
          path: '/index',                         //路由路径 全局不能重复
          name: 'index',                          //名字，全局不能重复，存在二级路由，则一级路由不能有名字
          label: '首页',                          // 页面名称(用于权限配置时 显示名称使用)
          component: _import('Index/Index'),      // 文件地址（此处对应的是views目录）
          meta:{
            title: '首页',                        //页面名称（横向teble标签切换）
            table: true,                          // 是否显示 teable 切换按钮
            display:true,                         //  是否在侧边导航菜单显示
            icon: 'el-icon-s-home'                //  侧边导航的icon图标
          }
        }
      2、包含二级路由（实际为三级路由）
        {
            id:20,                                  //ID全局不能重复
            path: '/admin',                         //路由路径 全局不能重复(此处为父级))
            label: '管理员列表',                     // 页面名称(用于权限配置时 显示名称使用)
            component: _import('admin/index'),      // 文件地址（此处对应的是views目录）注意：此文件下 应包含（router-view 标签 来显示子页面）
            meta:{
                title: '管理员列表',                   //页面名称（横向teble标签切换）
                table: true,
                display:true,                         //  是否在侧边导航菜单显示(注意 这里是父级，如果为false,则子级不在折叠)
                icon: 'el-icon-s-custom'              //  侧边导航的icon图标
            },
            children:[
              {
                id:21,                              //ID全局不能重复
                path: '/admin/index',               //路由路径 全局不能重复(此处为父级))
                label: '管理员列表',                  // 页面名称(用于权限配置时 显示名称使用)
                component: _import('admin/admin'),   // 文件地址（此处对应的是views目录）
                meta:{
                  title: '管理员列表',                //页面名称（横向teble标签切换）
                  table: true,                      // 是否显示 teable 切换按钮
                  display:true,                     //  是否在侧边导航菜单显示
                  icon:'el-icon-tickets'            //  侧边导航的icon图标
                }
              }
            ]
          }
 *
 *
 *
*/

let routerLists=[
  {
    id:0,
    path: '/tenant',
    component: Layout,
    // redirect: '/tenant',
    children: [{
      path: 'tenant',
      name: 'Tenant',
      component: () => import('@/views/tenant/index'),
      // meta: { title: 'Dashboard', icon: 'dashboard' }
      meta: { title: '租户管理', icon: 'table' ,role: ['RA0001','RA0002'] }
    }]
  },
  {
    id:10,
  path: '/schoolmgt',
  component: Layout,
  redirect: 'noRedirect',
  meta: { title: '校务管理', icon: 'school', role: ['RA0001','RA0002','RA0003']},
children: [
  {
    id:11,
    path: 'students',
    name: 'Students',
    component: () => import('@/views/schoolmgt/students'),
    meta: { title: '学生信息', icon: 'students' }
  },
  {
    id:12,
    path: 'teachers',
    name: 'Teachers',
    component: () => import('@/views/schoolmgt/teachers'),
    meta: { title: '教师管理', icon: 'teachers' }
  },
]
},
  {
    id:20,
    path: '/videomgt',
    component: Layout,
    children: [
      {
        id:21,
        path: 'index',
        name: 'Videomgt',
        component: () => import('@/views/videomgt/index2'),
        // path: 'http://ys.vlab2.chinamcloud.com/',
        meta: { title: '视频管理', icon: 'link',role: ['RA0001','RA0002','RA0003'] }
      }
    ]
  },
  {
    id:30,
    path: '/basicmgt',
    component: Layout,
    // redirect: '/basicmgt',
    redirect: 'noRedirect',
    // alwaysShow: true,
    meta: { title: '系统管理', icon: 'nested' },
    children: [
      {
        id:31,
        path: 'base',
        name: 'Base',
        component: () => import('@/views/basicmgt/base/index'),
        meta: { roles: ['RA0001'], title: '基础配置', icon: 'tree' }
      },
      {
        id:32,
        path: 'order',
        name: 'Order',
        component: () => import('@/views/basicmgt/order/order'),
        meta: { roles: ['RA0001','RA0002'], title: '套餐设置', icon: 'money' }
      },

      {
        id:33,
        path: 'usermgt',
        name: 'Usermgt',
        redirect: 'noRedirect',
        component: () => import('@/views/basicmgt/usermgt'),
        // component: Layout,
        meta: { title: '用户管理', icon: 'peoples' },
        children: [
          {
            id:331,
            path: 'consumer',
            name: 'Consumer',
            component: () => import('@/views/basicmgt/usermgt/consumer'),
            meta: { title: '用户列表', icon: 'user' }
          },
          {
            id:332,
            path: '/role',
            name: 'Role',
            component: () => import('@/views/permission/role'),
            meta: { title: '角色管理', icon: 'lock', roles: ['RA0001'],}
          }

        ]
      }

    ]

  },
  {
    id:40,
    path: '/viewmin',
    component: Layout,
    children: [
      {
        id:41,
        path: 'index',
        name: 'Viewmin',
        component: () => import('@/views/viewmin/index'),
        //path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: { title: '微门户', icon: 'component',role: ['RA0001','RA0002','RA0003'] }
      }
    ]
  },
  {
    id:50,
    path: '/dataview',
    component: Layout,
    redirect: '/',
    name: 'Dataview',
    // meta: { title: 'dataview', icon: 'example' },
    children: [
      {
        id:51,
        path: 'index',
        component: () => import('@/views/dataview/index'),
        meta: { title: '统计分析', icon: 'example',role: ['RA0001','RA0002','RA0003']}
      },
    ]
  },
]






let routerAlls=[
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
]

// 先把路由信息对象字符串化，然后去除component字段 ，再传递给 权限配置表
let routerListString =JSON.stringify(routerLists)
let src= routerListStr(routerListString)
 store.commit('serRouterList',src)


 let arr=[0,10,11,12,20,21,30,31,32,33,331,332,40,41,50,51]   //权限列表数组

 //根据权限配置表和路由信息对象 获取本用户的路由信息表，并添加到 home组件的二级路由里
routerAlls[0].children = routerListFun(arr,routerLists)

//根据权限配置表和路由信息对象 获取本用户的菜单列表
let mentParse =JSON.parse(JSON.stringify(src))
let menuList = routerListFun(arr,mentParse)
store.commit('setMents',menuList)






// 注册路由
const createRouter = ()=>new Router({
  mode: 'history',
  // base: process.env.BASE_URL,
  routes: [

  ]
})
const router = createRouter()
// 将筛选后的路由信息对象添加至路由表
router.addRoutes(routerAlls)


// 进行全局导航守卫
// routers.beforeEach((to,from,next)=>{
//
//       if(to.path != '/'){
//
//           let username=store.state.load.userList.username
//
//           if(username){
//             next()
//           }else{
//
//             next({
//               path:'/',
//               query:{
//                 path:to.path
//               }
//             })
//           }
//       }else{
//         next()
//       }
// })

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
export default router;
